#pragma once
#include <iostream>
#include <string>
#include <cstdio>
#include <functional>
using namespace std;

const string oper = "+-*/%";

class CalTask
{
    // 函数类型 参数(int,int,char) 返回值 int
    using func_t = function<int(int,int,char)>;
public:
    CalTask() {}
    CalTask(int x, int y, char op, func_t func)
    :_x(x), _y(y), _op(op), _callback(func) {}

    string operator()()
    {
        int result = _callback(_x, _y, _op);
        char buffer[1024];
        snprintf(buffer, sizeof buffer, "%d %c %d = %d", _x, _op, _y, result);
        return buffer;
    }
    string TasktoString()
    {
        char buffer[1024];
        snprintf(buffer, sizeof buffer, "%d %c %d = ?", _x, _op, _y);
        return buffer;
    }
private:
    int _x;
    int _y;
    char _op;
    func_t _callback;
};


int mymath(int x, int y, char op)
{
    int result = 0;
    switch (op)
    {
    case '+':
        result = x + y;
        break;
    case '-':
        result = x - y;
        break;
    case '*':
        result = x * y;
        break;
    case '/':
    {
        if (y == 0)
        {
            cerr << "div zero error!" <<endl;
            result = -1;
        }
        else
            result = x / y;
    }
        break;
    case '%':
    {
        if (y == 0)
        {
            cerr << "mod zero error!" <<endl;
            result = -1;
        }
        else
            result = x % y;
    }
        break;
    default:
        break;
    }

    return result;
}

class SaveTask
{
    // 函数类型 参数(string) 返回值 void
    using func_t = function<void(const string&)>;
public:
    SaveTask() {}
    SaveTask(const string &message, func_t func)
    : _message(message), _func(func) {}

    void operator()()
    {
        _func(_message);
    }
private:
    string _message;
    func_t _func;
};

void Save(const string &message)
{
    const string target = "./log.txt";
    FILE *fp = fopen(target.c_str(), "a+");  //可读可写 追加模式
    if(!fp)
    {
        cerr << "fopen error" << endl;
        return;
    }
    fputs(message.c_str(), fp);
    fputs("\n", fp);
    fclose(fp);
}